package com.alibaba.travel.article.service.impl;

import com.alibaba.travel.article.entity.ColumnArticleRelationEntity;
import com.alibaba.travel.article.interceptor.LoginInterceptor;
import com.alibaba.travel.article.service.ColumnArticleRelationService;
import com.alibaba.travel.common.utils.RRException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alibaba.travel.common.utils.PageUtils;
import com.alibaba.travel.common.utils.Query;

import com.alibaba.travel.article.dao.SpecialColumnDao;
import com.alibaba.travel.article.entity.SpecialColumnEntity;
import com.alibaba.travel.article.service.SpecialColumnService;
import org.springframework.util.ObjectUtils;


@Service("specialColumnService")
public class SpecialColumnServiceImpl extends ServiceImpl<SpecialColumnDao, SpecialColumnEntity> implements SpecialColumnService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecialColumnEntity> page = this.page(
                new Query<SpecialColumnEntity>().getPage(params),
                new QueryWrapper<SpecialColumnEntity>().eq("show_status", '0')
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPrivateArticlePage(Map<String, Object> params) {
        QueryWrapper<SpecialColumnEntity> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(LoginInterceptor.userIdLocal.get())) {
            throw new RRException("用户未登录");
        }
        wrapper.eq("user_id", LoginInterceptor.userIdLocal.get());
        LoginInterceptor.userIdLocal.remove();
        IPage<SpecialColumnEntity> page = this.page(
                new Query<SpecialColumnEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    private ColumnArticleRelationService columnArticleRelationService;
    /**
     * 专栏添加文章
     */
    @Override
    public boolean addArticle(Long columnId, Long[] articleIds) {
        Arrays.stream(articleIds).forEach(articleId ->{
            ColumnArticleRelationEntity relation = new ColumnArticleRelationEntity();
            relation.setArticleId(articleId);
            relation.setColumnId(columnId);
            columnArticleRelationService.save(relation);
        });
        return true;
    }

    /**
     * 专栏添加文章
     */
    @Override
    public boolean deleteArticle(Long columnId, Long[] articleIds) {
        Arrays.stream(articleIds).forEach(articleId ->{
            LambdaQueryWrapper<ColumnArticleRelationEntity> wrapper = new LambdaQueryWrapper<ColumnArticleRelationEntity>()
                    .eq(ColumnArticleRelationEntity::getColumnId, columnId)
                    .eq(ColumnArticleRelationEntity::getArticleId, articleId);
            int delete = columnArticleRelationService.getBaseMapper().delete(wrapper);
        });
        return true;
    }

}